/*
 * @Author: your name
 * @Date: 2020-05-06 10:46:03
 * @LastEditTime: 2020-08-07 10:46:07
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \truck\platform\include\global_common.h
 */
#ifndef __XE_COMMON_H_
#define __XE_COMMON_H_
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <semaphore.h>
#include <unistd.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>


#include <sys/syscall.h>
#include <sys/epoll.h>
#include <sys/prctl.h>
#include <fcntl.h>

#include <stdbool.h>


// #include "list.h"
#include "xeLOG_internal.h"
#include "cJSON.h"

#include "mongoose.h"

#include "libvector.h"

#ifndef _CORE_GETTID
#include <sys/syscall.h>
#define _CORE_GETTID()	syscall(SYS_gettid)
#endif

enum req_type{
// avplay
  HTTP_REQ_SPEED_CTRL =5,
  HTTP_REQ_AVPLAY_PAUSE =6,
  HTTP_REQ_AVPLAY_RESUME =7,
  HTTP_REQ_AVPLAY_STOP =8,
  HTTP_REQ_AV_DL_RATE =9,

// region mgnt
  HTTP_REQ_REGION_NEW = 20,
  HTTP_REQ_REGION_DELETE =21,
  HTTP_REQ_REGION_MODIY = 22,
  HTTP_REQ_REGION_QUERY = 23,
  HTTP_REQ_REGION_QUERY_ALL = 24,
};



#undef	TINY_MAX
#define TINY_MAX(a, b)  (((a) > (b)) ? (a) : (b))

#undef	TINY_MIN
#define TINY_MIN(a, b)  (((a) < (b)) ? (a) : (b))


#define FT_TASK_PROTO_UDP	1
#define FT_TASK_PROTO_TCP	2
#define FT_TASK_PROTO_SCTP	3
#define FT_TASK_PROTO_UDGR	4
#define FT_TASK_PROTO_DEC	5
#define FT_TASK_PROTO_RTP  6


#define FT_TASK_MODE_PORT      1
#define FT_TASK_MODE_PASSIVE   2


#define __free( p ) do { if(p){free(p);p=NULL;} }while(0)

#define check_return_val(retval)	\
do{									\
	if(retval != 0){				\
		xeLOG_ERR("error");			\
	}				\
}while(0);

#define check_return_point(p_val)	\
do{									\
	if(p_val == NULL){				\
		xeLOG_ERR("error");		\
	}				\
}while(0);

#define err_msg()	\
do{								\
		STD_print("error");		\
		xeLOG_ERR("error");		\
}while(0);

#define __check_return_val_return(retval,msg)	\
do{									\
	if(retval != 0){				\
		xeLOG_ALERT("error,%s",msg);	\
		STD_print("error,%s",msg);			\
		return retval;	\
	}				\
}while(0);

#define __check_return_val(retval,error_flag,msg,action)	\
do{									\
	if(retval != 0){				\
		xeLOG_ERR("error,%s",msg);			\
        sprintf(errReason,"%s",msg);    \
        error_flag = 1;    \
        goto action;  \
	}				\
}while(0);
#define __check_return_point(p_val,error_flag,msg,action)	\
do{									\
	if(!p_val){				\
		xeLOG_ERR("%s",msg);		\
        error_flag =1 ;       \
        sprintf(errReason,"%s is NULL",msg);\
        goto action;  \
	}				\
}while(0);


#define __check_return_cstring(p_val,error_flag,msg,action)	\
do{									\
	if(!strcmp(p_val,"")){				\
		xeLOG_ERR("error, %s is empty",msg);		\
        error_flag = 1 ;       \
        sprintf(errReason,"%s is empty",msg);\
        goto action;  \
	}				\
}while(0);

#define __prase_cjson_string(p_val,error_flag,msg,action)	\
do{									\
	memset(p_val,0,sizeof(p_val));  \
    cJSON_GetObjectValue(p_val,sizeof(p_val),root,msg); \
    __check_return_cstring(p_val,error_flag,msg,action);\
}while(0);



#define ALIGN_INT(a,size)   (((a/size)+(a%size?1:0))*512)

#define HISOME_LEN_16 (16)
#define HISOME_LEN_32 (32)
#define HISOME_LEN_64 (64)
#define HISOME_LEN_128 (128)
#define HISOME_LEN_256 (256)
#define HISOME_LEN_512 (512)
#define HISOME_LEN_1024 (1024)
#define HISOME_LEN_2048 (2048)
#define HISOME_LEN_4096 (4096)
#define HISOME_LEN_8192 (8192)

enum XEREC_SIZE
{
	XEREC_SIZE_NONE=0,
	XEREC_SIZE_AUTO,
	
	XEREC_SIZE_64KB=(64<<10),
	XEREC_SIZE_128KB=(128<<10),
	XEREC_SIZE_256KB=(256<<10),
	XEREC_SIZE_384KB=(384<<10),
	XEREC_SIZE_512KB=(512<<10),
	XEREC_SIZE_768KB=(768<<10),
	
	XEREC_SIZE_1MB=(1<<20),
	XEREC_SIZE_2MB=(2<<20),
	XEREC_SIZE_3MB=(3<<20),
	XEREC_SIZE_4MB=(4<<20),
	XEREC_SIZE_5MB=(5<<20),
	XEREC_SIZE_6MB=(6<<20),
	XEREC_SIZE_7MB=(7<<20),
	XEREC_SIZE_8MB=(8<<20),
	XEREC_SIZE_9MB=(9<<20),
	
	XEREC_SIZE_10MB=(10<<20),
	XEREC_SIZE_11MB=(11<<20),
	XEREC_SIZE_12MB=(12<<20),
	XEREC_SIZE_13MB=(13<<20),
	XEREC_SIZE_14MB=(14<<20),
	XEREC_SIZE_15MB=(15<<20),
	XEREC_SIZE_64MB=(64<<20),
	
	XEREC_SIZE_128MB=(128<<20),
	XEREC_SIZE_256MB=(256<<20),
	XEREC_SIZE_512MB=(512<<20),
	XEREC_SIZE_768MB=(768<<20),
	
	XEREC_SIZE_1GB=(1<<30),

	XEREC_SIZE_2GB=(2<<30),
	XEREC_SIZE_3GB=(3<<30),
};
// #define PER_IPC_SPACE XEREC_SIZE_2GB
// #define PER_IPC_SPACE (100<<30)
#define PER_IPC_SPACE_GB (200)
/**
 * @Date: 2020-05-13 13:49:30
 * @Description: describe sysmgnt.db
 * @Return: 
 */
typedef struct ipc_res{	
	char collector_ip[32];
	char collector_mac[32];
	char collector_sn[HISOME_LEN_128];

	char ipc_sn[HISOME_LEN_64];
	char ipc_mac[HISOME_LEN_64];
	char ipc_ipaddr[HISOME_LEN_64];//reserver
	char ipc_name[HISOME_LEN_64];//reserver

	char harddisk_sn[HISOME_LEN_128];
	char harddisk_dir[HISOME_LEN_128];
	char storage_path[HISOME_LEN_128];
	char storage_db[HISOME_LEN_128];

	char file_name[HISOME_LEN_256];
	long long  file_len;
	long long  rec_offset;

	time_t start_time;
	time_t end_time;
}t_ipc_query_info;

typedef struct _space_res{	
	char harddisk_sn[HISOME_LEN_128];
	char harddisk_dir[HISOME_LEN_128];

	int dir_limit;
	int dir_used;
	int dir_free;
}t_space_query_info;

/**end describe sysmgnt.db **/


/**
 * @Date: 2020-05-13 13:48:50
 * @Description: describe ipc.db
 * @Return: 
 */
typedef struct ipc_file_tbl_des{	
	char file_name[HISOME_LEN_256];
	char detail_info_name[HISOME_LEN_256];

	time_t start_time;
	time_t end_time;
	int duration;

	long long file_size;//ocupy psace acctruaryly
	long long file_space;//need ocupy space

}t_ipc_file_tbl_des;

typedef struct ipc_statisics_tbl_des{	
	int num_files;
	time_t start_time;
	time_t end_time;
	long long total_size;//no use now
	long long total_space;//space used
	long long space_limit;//2G
}t_ipc_statis_des;

/**end describe ipc.db **/

typedef struct file_opt_info{
	t_ipc_file_tbl_des file_des;
	int start_offset_sec;
	int end_offset_sec;
	long long start_offset_bytes;
	long long end_offset_bytes;
}t_file_opt_info;

typedef struct ipc_query_result{	
	int durNum;
	time_t start_time,end_time;
	vector_t *p_vector;//t_file_opt_info
}t_ipc_query_result;

#define AV_UPLOAD_URL "/download/setUp"

#define TINY_LOGIN	"/login"
#define TINY_LOGOUT	"/logout"
#define TINY_USER_ALIVE	"/useralive"

#define AV_DOWNLOAD "/file/download"
#define AV_PLAY "/file/avplay"


#define AV_PLAY_SPEED_CTL "/file/speedctl"
#define AV_PLAY_PAUSE "/file/play/pause"
#define AV_PLAY_RESUME "/file/play/resume"
#define AV_PLAY_STOP "/file/play/stop"
#define AV_DL_RATE "/file/rate"


#define AV_QUERY_IPCLIST "/query/ipclist"
#define AV_QUERY_FLIELIST "/query/filelist"
#define AV_DELETE_DEVICE "/delete/device"

#define TINY_REGIONMGNT "/regionmgnt"
#define TINY_REGION_QUERYALL "/region_query_all"
/**
 * @Date: 2020-05-06 11:35:40
 * @Description: gy
 * @Return: 
 */

#define HISOME_PREFIX "/HISOMEAPI"
#define HISOME_PREFIX_NOJSON "/NOJSON_HISOMEAPI"
#define  DBVERSION_PREFIX "HSME_DBV_"


#define LINE (__LINE__)

#define HOST "127.0.0.1"

#define IPC_DB_DETAIL_NAME "detailinfo"

enum _httpMethod{HTTP_METHOD_GET=0,HTTP_METHOD_POST,HTTP_METHOD_DELETE,HTTP_METHOD_QUERY};
typedef enum  _httpMethod HTTP_Method;


typedef struct _urlMapstruct{
    char url[HISOME_LEN_128];
    char ip[HISOME_LEN_128];
    HTTP_Method method;
    int _isJson;
}UrlMapStruct;
typedef struct _myHttpReq{
    struct mg_connection *nc;
    struct http_message *hm;
}MyHttpReq;

typedef struct _myHttpPack{
    UrlMapStruct urlmap;
    MyHttpReq httpReq;
}MyHttpPack;

typedef void(*http_handle)(void * data);
typedef struct urlmap{
    char h_url[1024];
    http_handle m_get;
    http_handle m_post;
    http_handle m_delete;
    http_handle m_query;
    char remarks[1024];
}UrlMap;



#ifdef CONFIG_USE_EXTERNAL_XELOG
    #define PRTLOG_ERR xeLOG_ERR 
    #define PRTLOG_DBG xeLOG_DEBUG 
    #define PRTLOG_INFO xeLOG_INFO 
#else
    #define PRTLOG_ERR(fmt, args...) printf("%s-%s()-%d: "fmt"\n", __FILE__, __FUNCTION__,__LINE__, ##args); 
    #define PRTLOG_DBG(fmt, args...) printf("%s-%s()-%d: "fmt"\n", __FILE__, __FUNCTION__,__LINE__, ##args); 
    #define PRTLOG_INFO(fmt, args...) printf("%s-%s()-%d: "fmt"\n", __FILE__, __FUNCTION__,__LINE__, ##args); 
#endif

void   com_initSignals();
int    com_getCPURate();
int    com_getMemRate(char *process);
int    com_getFanSpeed();
int    com_getCPUTemp();
int    com_pthread_mutex_lock(pthread_mutex_t *mutex,char *mutexName,const char * filename,int line);
void   com_pthread_mutex_unlock(pthread_mutex_t *mutex,char *mutexName,const char * filename,int line);
void   com_fileCopy(const char *src,const char *dst);
void   com_httpPack(struct mg_connection *nc,cJSON *json);
void   com_strtokSup(char * strlist);
time_t com_localTimeStr2Int(const char * ts);
char * com_localTimeInt2Str(time_t ti,char *dst);

long long get_file_size(const char *filename);
void get_file_name(char *file,char *file_name);

int tiny_get_unique_id(int *id);
extern int tiny_http_main();


extern char * tb_uuid4_make_cstr(char uuid_cstr[37], const char* name);
#endif//__XE_COMMON_H_

